package com.extsci.webmail.web.service;

import com.extsci.webmail.common.entity.http.ObjectRestResponse;
import com.extsci.webmail.common.entity.http.Pageable;
import com.extsci.webmail.common.entity.http.ResponseStatus;
import com.extsci.webmail.common.entity.sensitive.Sensitive;
import com.extsci.webmail.common.type.PositionType;
import com.extsci.webmail.common.util.CommonUtils;
import com.extsci.webmail.exception.SensitiveException;
import com.extsci.webmail.mapper.SensitiveMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 敏感词服务
 * @author ccoke
 */
@Service
public class SensitiveService {
    private static final int DEFAULT_PAGESIZE = 10;
    private static final int DEFAULT_CURRENTPAGE = 0;

    @Value("${webmail.harmony}")
    private String harmony;

    private final SensitiveMapper sensitiveMapper;

    @Autowired
    public SensitiveService(SensitiveMapper sensitiveMapper) {
        this.sensitiveMapper = sensitiveMapper;
    }

    /**
     * 获取所有敏感词
     */
    public Pageable<Sensitive> getSensitives(int currentPage, int pageSize, String keyword) {
        if (pageSize < 1){
            pageSize = DEFAULT_PAGESIZE;
        }
        if (currentPage < 0) {
            currentPage = DEFAULT_CURRENTPAGE;
        }
        // 获取搜索总数
        Integer totalCount = sensitiveMapper.selectSensitivesCount(keyword);
        if (totalCount < 1){
            return new Pageable<>(totalCount, currentPage, new ArrayList<>());
        }
        int totalPages = (int)Math.ceil(1.0 * totalCount / pageSize);
        if (currentPage >= totalPages){
            // 如果页数溢出则跳到最后一页
            currentPage = totalPages - 1;
        }
        List<Sensitive> sensitives = sensitiveMapper.selectSensitives(currentPage * pageSize, pageSize, keyword);
        return new Pageable<>(totalPages, currentPage, sensitives);
    }

    /**
     * 添加敏感词
     */
    public ObjectRestResponse<Sensitive> addSensitive(Sensitive sensitive) {
        sensitive.setUuid(CommonUtils.createUUID()).setCreateAt(new Date());
        sensitiveMapper.insertSensitive(sensitive);
        return new ObjectRestResponse<>(ResponseStatus.SUCCESS, sensitive);
    }

    /**
     * 修改敏感词
     */
    public ObjectRestResponse<Sensitive> modifySensitive(Sensitive sensitive) {
        if (sensitiveMapper.updateSensitive(sensitive) != 1){
            return new ObjectRestResponse(ResponseStatus.NOTFOUNT);
        }
        return new ObjectRestResponse<>(ResponseStatus.SUCCESS, sensitive);
    }

    /**
     * 移除敏感词(多选)
     */
    public ObjectRestResponse<String> removeSensitives(List<String> uuids) {
        uuids.stream().forEach(uuid -> {
            sensitiveMapper.deleteSensitiveByUuid(uuid);
        });
        return new ObjectRestResponse<>(ResponseStatus.SUCCESS);
    }


    /**
     * 进行过滤
     */
    public String inspect(String str, PositionType position) throws SensitiveException {
        List<Sensitive> sensitives = null;
        switch (position){
            case SUBJECT:
                sensitives = sensitiveMapper.selectSensitivesByPosition(1, 3, 5, 7);
                break;
            case BODY:
                sensitives = sensitiveMapper.selectSensitivesByPosition(2, 3, 6, 7);
                break;
            case ATTACHMENT:
                sensitives = sensitiveMapper.selectSensitivesByPosition(4, 5, 6, 7);
                break;
            default:
        }
        for (Sensitive sensitive : sensitives) {
            if (str.contains(sensitive.getWord())){
                if (sensitive.getRegular() == 1){
                    // TODO 拦截
                    throw new SensitiveException(sensitive.getWord());
                }
                str = str.replace(sensitive.getWord(), harmony);
            }
        }
        return str;
    }

}
